﻿using DMSkin.WPF;
using log4net;
using MXComponent.Api;
using MXComponent.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MXComponent
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : DMSkinSimpleWindow
    {
        public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public LoginWindow()
        {
            InitializeComponent();
        }

        private void btnok_Click(object sender, RoutedEventArgs e)
        {
            string usercode = string.Empty;
            string password = string.Empty;
            if(rbadmin.IsChecked == true)
            {
                usercode = "admin";
            }
            else
            {
                usercode = "operator";
                password = "123456";
            }
            
            if (password == string.Empty)
            {
                if (string.IsNullOrWhiteSpace(this.txtpassword.Password))
                {
                    MessageBox.Show("请输入密码！");
                    this.txtpassword.Focus();
                    return;
                }
                password = this.txtpassword.Password.Trim();
            }
            try
            {
                userDB userdb = new userDB();
                GlobalClass.user = userdb.CheckLogin(usercode, password);
                if (GlobalClass.user != null)
                {
                    MainWindowApp mw = new MainWindowApp();
                    mw.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！");
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void btnclose_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void txtpassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                this.btnok_Click(sender, e);//触发button事件
            }
        }

        private void txtusername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                this.txtpassword.Focus();
            }
        }

        private void DMSkinSimpleWindow_Loaded(object sender, RoutedEventArgs e)
        {
            PLCClass.Init();
        }

        private void rbuser_Checked(object sender, RoutedEventArgs e)
        {
            if (passwordpanel != null)
            {
                passwordpanel.Visibility = Visibility.Hidden;


            }
        }

        private void rbadmin_Checked(object sender, RoutedEventArgs e)
        {
            if (passwordpanel != null)
            {
                passwordpanel.Visibility = Visibility.Visible;
                this.txtpassword.Focus();
            }
        }
    }
}